var start_event = function(){

    if($('#dropZone').attr('rest1')=='' &&  $('#dropZone').attr('rest2')=='') { 
    
        $('#dropZone').hide();
    }
}

var str_tpl = '<div class="drop-zone"  id="dropZone"><div class="caption">Drag and drop files here</div></div>';
$("#EditView_tabs").before(str_tpl);

setInterval(start_event,2000);

var FileManager = $.Class.create({
	
	init: function(parent, name)
	{	
		$("#dropZone").hide();
		$(".loader").hide();
		
		this.body_el = $("body");
		
		this.body_el.bind("dragleave", this._leave_body.bind(this));
		this.body_el.bind("dragover", this._over_body_block.bind(this));
		this.body_el.bind("dragenter", this._dragenter_body.bind(this));   

		
		if($("#uploadfile").attr('type') !='hidden') {
	
			this.nid = $("input[name='record']").val();
			this.files = {};
			this.el = $("#filesList")
			this.body_el.bind("dragover", this._over_body.bind(this));
			this.url = "index.php?module=ddrls_DragandDrop&action=upload_files_document&sugar_body_only=true&nid="+this.nid;
			
			var uploader = new DragUpload("dropZone", 
										{
											url: this.url,
											mousein_class: "dragover",
											onDropFile: this.showFileInList.bind(this),
											onProgress: this.onProgress.bind(this),
											onComplete: this.onLoad.bind(this)
										});
										
			this.initFiles();
		}	
	},
    leave_hide : function()
    {
        $("#dropZone").attr('rest1','none'); 
        
        if($('#dropZone').attr('rest1')=='none' &&  $('#dropZone').attr('rest2')=='none') { 
            $("#dropZone").attr('rest1',''); 
            $("#dropZone").attr('rest2',''); 
        };
        
        var sttr = $('#dropZone').attr('rest1');
        var sttr2 = $('#dropZone').attr('rest2');
        
		return false;
    },
    _over_body : function(e)
    {
        $("#dropZone").attr('rest1','block1'); 
        $("#dropZone").attr('rest2','none'); 
         
        if($("#dropZone").css('display')=='none') 
        {
            $("#dropZone").show(); 
        } 
		
        var dt = e.originalEvent.dataTransfer;
        if (!dt) { return; }
        dt.dropEffect = 'none';
        return false;
        
    },
    _over_body_block : function(e)
    {
        var dt = e.originalEvent.dataTransfer;
        if (!dt) { return; }
        dt.dropEffect = 'none';
        return false;
        
    }, 
    _dragenter_body : function(e)
    {
        return false;
    }, 
    _leave_body : function(e)
    {
         var dt = e.originalEvent.dataTransfer;
		 if(!dt) return;
		 //FF
		 if(dt.types.contains&&!dt.types.contains("Files")) return;
		 //Chrome
		 if(dt.types.indexOf&&dt.types.indexOf("Files")==-1) return;
		 
		 if($.browser.webkit) dt.dropEffect = 'copy';      
    
    
        return this.leave_hide();
    },
	createFile: function(name)
	{
		var file = new FileLine(this.el, name);
		this.files[name] = file;
		return file;
	},
	initFiles: function()
	{

 		this.el.find(".b-file").each(function(i, el){
			var fileName = $("#old_file_name").text();
			var file = this.createFile(fileName);
			file.el = $(el);
			file.bind();
			
			
		}.bind(this))
	},
	deleteFile: function(file)
	{
		var name = file.name;
		delete this.files[name];
		file.remove();
		this.sendDelete(name);		
	},
	sendDelete: function(name)
	{
		$.ajax({
			  type: 'POST',
			  url: this.url+"?delete="+name,
			  
			  complete: function(r, textStatus, errorThrown){ 
			 	 
			  }
			});
	},
	showFileInList: function(e, fileName){
			
 		var file = this.createFile(fileName);
		file.render(); 
        
        var str_input_elem = '<input type="hidden" id="uploadfile" value="'+fileName+'" onchange="setvalue(this);" maxlength="" size="30" name="uploadfile" tabindex="100">'
        $("#escaped_document_name").val(fileName);
        $("#old_file_name").html(fileName); 
        $("#uploadfile").remove(); 
        $("#old_file_name").after(str_input_elem);
	},
	onProgress: function(e, fileName) { 
        
		this.files[fileName].showProgress(e.loaded, e.total);
			
	},
	onLoad: function(e, fileName){
	 	var data = $.parseJSON(e.target.responseText);
		var newFileName = data.file;
        
		this.files[fileName].finishLoading(newFileName);
	}
});